myApp.onPageInit('file-list', function (page) {
    console.log("onPageInit " + page.name);

    var list = $$(page.container).find('.list-block'),
        ul = list.find('ul'),
        empty = $$(page.container).find('.empty-view'),
        refreshContainer = $$(page.container).find('.pull-to-refresh-content');
    list.data('request', null);
    list.data('lastpage', false);
    list.data('nextpage', 0);

    function loadPage() {
        var refresing = list.data('nextpage') == 0,
            req = true;
        $$.getJSONP(SERVER_PATH + 'getXxwjList.do', {num: PAGE_SIZE, start: (list.data('nextpage') * PAGE_SIZE)}, function (json) {
            if (json.code == 1) {
                list.data('nextpage', list.data('nextpage') + 1);

                if (refresing) {
                    list.find('ul').children().remove();
                }

                $$.each(json.results, function (index, value) {
                    if (value.isLastPage) {
                        list.data('lastpage', true);
                    }

                    preprocessData('xxwjList', value);
                    var html = Template7.templates.xxwjTemplate(value);
                    list.find('ul').append(html);
                });
                empty[list.find('ul').children().length > 0 ? 'hide' : 'show']();
            }

            list.data('request', null);
            if (refresing) {
                myApp.pullToRefreshDone();
            }
        });
        list.data('request', req);
    }
    
    ul.on('click', 'a.item-link', function () {
        $$(this).find('.badge').hide();
    });
    
    $$(page.container).find('.infinite-scroll').on('infinite', function () {
        if (list.data('lastpage') || list.data('request')) {
            return;
        }

        loadPage();
    });

    refreshContainer.on('refresh', function () {
        var request = list.data('request');
        if (request) {
            request.abort();
        }
        list.data('request', null);
        list.data('lastpage', false);
        list.data('nextpage', 0);
        loadPage();
    });
    myApp.pullToRefreshTrigger(refreshContainer);
});

myApp.onPageBeforeInit('file-detail', function (page) {
    if (myApp.padApp) {
        $$(page.navbarInnerContainer).find('.left').css('visibility', 'hidden');
    }
});

myApp.onPageAfterBack('file-detail', function (page) {
	$$('.page[data-page="file-list"] .pull-to-refresh-content').trigger('refresh');
});

myApp.onPageInit('file-detail', function (page) {
    console.log("onPageInit " + page.name);
    var params = page.query;

    $$.getJSONP(SERVER_PATH + 'getXxwjDetail.do', {pkId: params.id}, function (json) {
        if (json.code == 1) {
            var detail = json.results,
                attachList = detail.attachList,
                list1 = $$(page.container).find('.list-block.message-list ul'),
                list2 = $$(page.container).find('.list-block.files-block ul');	
             

            list1.find('.item-title').text(detail.title);

            var divclkme = $$(page.container).find('.item-title.clkme');
            divclkme.click(function () {
	              if(typeof clientType!=="undefined"){         
	                  if(clientType==1){  //ios
	                      document.addEventListener("deviceready", function(){
	          				TrunPage.openWebView(detail.content,'html','正文');
	                      }, false);
	                  }else{
	                	  	TrunPage.openWebView(detail.content,'html','正文');
	                  }
	              }
            });

            $$.each(attachList, function (index, value) {
                value.name = value.attchName;
                value.suffix = value.suffix || 'file';

                var html = Template7.templates.fileTemplate(value);
                list2.append(html);
            });
            var content = list1.find('.message-content');
            var h = list2.offset().top - content.offset().top - 9 - 5;
            content.css('height', h + 'px');
        }
    });
});
